/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmGiaTienBao.java
 *
 * Created on May 26, 2011, 3:16:47 PM
 */

package Presentation;

import RSCall.BaoRSc;
import RSCall.GiaTienQCBaoRSc;
import Entities.BAO;
import Entities.GIATIEN_QC_BAO;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author VooKa
 */
public class frmGiaTienBao extends javax.swing.JInternalFrame {

    /** Creates new form frmGiaTienBao */
    public frmGiaTienBao() {
        initComponents();
    }

    private void clear() {
        tbxMauSac.setText("");
        tbxKhoIn.setText("");
        tbxGhiChu.setText("");
        tbxViTri.setText("");
        tbxGia.setText("");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        tbxViTri = new javax.swing.JTextField();
        tbxKhoIn = new javax.swing.JTextField();
        tbxMauSac = new javax.swing.JTextField();
        tbxGia = new javax.swing.JTextField();
        tbxGhiChu = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        btnAdd = new javax.swing.JButton();
        btnUpdate = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblGiaTienBao = new javax.swing.JTable();

        setTitle(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("titlefrmGiaTienBao"));
        setPreferredSize(new java.awt.Dimension(605, 520));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel2.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel2.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("location"));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel3.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("printsize"));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel4.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("color"));

        jLabel5.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel5.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("cost"));

        tbxGia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                tbxGiaKeyTyped(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        jLabel6.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("note"));

        btnAdd.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnAdd.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("insert"));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnUpdate.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnUpdate.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("update"));
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        btnDelete.setFont(new java.awt.Font("Arial", 1, 14)); // NOI18N
        btnDelete.setText(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("delete"));
        btnDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDeleteActionPerformed(evt);
            }
        });

        tblGiaTienBao.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tblGiaTienBao.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblGiaTienBaoMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblGiaTienBao);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(btnAdd))
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(40, 40, 40)
                        .addComponent(btnDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(tbxGia)
                    .addComponent(tbxMauSac)
                    .addComponent(tbxKhoIn)
                    .addComponent(tbxViTri)
                    .addComponent(tbxGhiChu))
                .addGap(200, 200, 200))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addComponent(jScrollPane1)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(tbxViTri, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tbxKhoIn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(tbxMauSac, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(tbxGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(tbxGhiChu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAdd)
                    .addComponent(btnUpdate)
                    .addComponent(btnDelete))
                .addGap(29, 29, 29)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.LoadData();
        this.LoadCombobox();
    }//GEN-LAST:event_formInternalFrameOpened

    private void tblGiaTienBaoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblGiaTienBaoMouseClicked
        // TODO add your handling code here:
        int index = this.tblGiaTienBao.getSelectedRow();
        tbxViTri.setText(this.tblGiaTienBao.getValueAt(index, 2).toString());
        tbxKhoIn.setText(this.tblGiaTienBao.getValueAt(index, 3).toString());
        tbxMauSac.setText(this.tblGiaTienBao.getValueAt(index, 4).toString());
        tbxGia.setText(this.tblGiaTienBao.getValueAt(index, 5).toString());
        tbxGhiChu.setText((String)this.tblGiaTienBao.getValueAt(index, 6));
    }//GEN-LAST:event_tblGiaTienBaoMouseClicked

    // Xử lý thêm đối tượng
    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        String viTri = tbxViTri.getText().trim();
        String khoIn = tbxKhoIn.getText().trim();
        String mauSac = tbxMauSac.getText().trim();
        String ghiChu = tbxGhiChu.getText().trim();
        String giaTien = tbxGia.getText().trim();
           if(viTri.isEmpty())
                JOptionPane.showMessageDialog(this,
                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notenterlocation"));
            else
                if(khoIn.isEmpty())
                    JOptionPane.showMessageDialog(this,
                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notenterprintsize"));
                else
                    if(mauSac.isEmpty())
                        JOptionPane.showMessageDialog(this,
                                java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notentercolor"));
                    else
                        if(giaTien.isEmpty())
                            JOptionPane.showMessageDialog(this,
                                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notentercost"));
                        else
                        {
                            try
                            {
                                GiaTienQCBaoRSc _giaTien = new GiaTienQCBaoRSc();
                                Float giaTienBao = Float.MIN_VALUE;
                                try{
                                    giaTienBao = Float.parseFloat(giaTien);
                                    _giaTien.Insert(viTri, khoIn, mauSac, ghiChu, giaTienBao);
                                    this.LoadData();
                                    clear();
                                    JOptionPane.showMessageDialog(this,
                                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("insertedGiaTienBao"));
                                }catch(NumberFormatException e){
                                    JOptionPane.showMessageDialog(this,
                                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("nhapsoGiaTien"));
                                }
                            }catch(Exception e){
                                JOptionPane.showMessageDialog(this, e.toString());
                            }
                        }
    }//GEN-LAST:event_btnAddActionPerformed

    // Xử lý update
    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        // TODO add your handling code here:
        String viTri = tbxViTri.getText().trim();
        String khoIn = tbxKhoIn.getText().trim();
        String mauSac = tbxMauSac.getText().trim();
        String ghiChu = tbxGhiChu.getText().trim();
        String giaTien = tbxGia.getText().trim();
        if(this.tblGiaTienBao.getSelectedRow()==-1)
            JOptionPane.showMessageDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mustselectGiaTien"));
        else
            if(mauSac.isEmpty())
                    JOptionPane.showMessageDialog(this,
                            java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notentercolor"));
                else
                    if(khoIn.isEmpty())
                        JOptionPane.showMessageDialog(this,
                                java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notenterprintsize"));
                    else
                        if(viTri.isEmpty())
                            JOptionPane.showMessageDialog(this,
                                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notenterlocation"));
                        else
                            if(giaTien.isEmpty())
                                JOptionPane.showMessageDialog(this,
                                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("notentercost"));
                            else
                            {
                                try
                                {
                                    GiaTienQCBaoRSc _giaTien = new GiaTienQCBaoRSc();
                                    String maGiaTien = tblGiaTienBao.getValueAt(tblGiaTienBao.getSelectedRow(), 1).toString();
                                    Float giaTienBao = Float.MIN_VALUE;
                                    try{
                                        giaTienBao = Float.parseFloat(giaTien);
                                        _giaTien.Update(maGiaTien, viTri, khoIn, mauSac, ghiChu, giaTienBao);
                                        this.LoadData();
                                        clear();
                                        JOptionPane.showMessageDialog(this,
                                                java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("updatedGiaTienBao"));
                                    }catch(NumberFormatException e){
                                        JOptionPane.showMessageDialog(this,
                                                java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("nhapsoGiaTien"));
                                    }
                                }catch(Exception e){
                                    JOptionPane.showMessageDialog(this, e.toString());
                                }
                            }
    }//GEN-LAST:event_btnUpdateActionPerformed

    private void btnDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDeleteActionPerformed
        // TODO add your handling code here:
        if(this.tblGiaTienBao.getSelectedRow()==-1)
            JOptionPane.showMessageDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("mustselectGiaTien"));
        else
        {
            int n = JOptionPane.showConfirmDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("really delete"),
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("warn"),JOptionPane.YES_NO_OPTION);
            if(n==0)
            {
                try
                {
                    GiaTienQCBaoRSc _giaTien = new GiaTienQCBaoRSc();
                    _giaTien.Delete((String)this.tblGiaTienBao.getValueAt(this.tblGiaTienBao.getSelectedRow(), 1));
                    this.LoadData();
                    this.clear();
                    JOptionPane.showMessageDialog(this,
                    java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("deleted"));
                }catch(Exception e){
                    JOptionPane.showMessageDialog(this, e.toString());
                }
            }
        }
    }//GEN-LAST:event_btnDeleteActionPerformed

    private void tbxGiaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tbxGiaKeyTyped
        // TODO add your handling code here:
        String decimal = "1234567890.";
        int key = evt.getKeyChar();
        if(key!=KeyEvent.VK_BACK_SPACE&&key!=KeyEvent.VK_DELETE&&key!=KeyEvent.VK_ENTER&&key!=KeyEvent.VK_END&&key!=KeyEvent.VK_HOME&&key!=KeyEvent.VK_TAB)
        {
            int flag=0;
            if(decimal.indexOf(key)==-1)
            {
                flag++;
                JOptionPane.showMessageDialog(this,
                        java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("nhapsoGiaTien"));
            }
            if(flag>0)
                evt.consume();
        }
    }//GEN-LAST:event_tbxGiaKeyTyped
    private void LoadData(){
        try
        {
            GiaTienQCBaoRSc _giaTien = new GiaTienQCBaoRSc();
            ArrayList<GIATIEN_QC_BAO> lst = _giaTien.GetAllRows();
            DefaultTableModel dtm = new DefaultTableModel(){
                @Override
                public boolean  isCellEditable(int x,int y){
                    return false;
                }
            };
            dtm.addColumn("STT");
            dtm.addColumn("Mã Giá Tiền");
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("location"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("printsize"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("color"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("cost"));
            dtm.addColumn(java.util.ResourceBundle.getBundle("Presentation/" + frmMain.resource).getString("note"));
            for(int i=0;i<lst.size();i++)
            {
                Vector _v = new Vector();
                GIATIEN_QC_BAO _gia = lst.get(i);
                _v.add(i+1);
                _v.add(_gia.getMaGiaTien());
                _v.add(_gia.getViTri().trim());
                _v.add(_gia.getKhoIn().trim());
                _v.add(_gia.getMauSac().trim());
                _v.add(_gia.getGiaTien());
                _v.add(_gia.getGhiChu().trim());
                dtm.addRow(_v);
            }
            this.tblGiaTienBao.setModel(dtm);
            this.tblGiaTienBao.getColumnModel().getColumn(0).setPreferredWidth(30);
            this.tblGiaTienBao.getColumnModel().getColumn(6).setPreferredWidth(140);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
    private void LoadCombobox(){
        try
        {
            BaoRSc _bao = new BaoRSc();
            ArrayList<BAO> lst = _bao.GetAllRows();
            for(int i=0;i<lst.size();i++)
            {
                BAO _b = lst.get(i);
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblGiaTienBao;
    private javax.swing.JTextField tbxGhiChu;
    private javax.swing.JTextField tbxGia;
    private javax.swing.JTextField tbxKhoIn;
    private javax.swing.JTextField tbxMauSac;
    private javax.swing.JTextField tbxViTri;
    // End of variables declaration//GEN-END:variables

}
